let send = document.querySelector("#send");
let inp = document.querySelector('input');
let tbody = document.querySelector("tbody");
let audio = document.querySelector("audio");
let prev = document.querySelector("#prev");
let next = document.querySelector("#next");
let c = 0;
let d = 15;
let index = 0;

send.onclick = function() {

    let a = inp.value;
    fetch(`https://music.0melon0.cn/search?keywords=${a}&offset=${c}&limit=${d}`, {
        method: "GET", //type请求类型；
        headers: { //设置请求头，表单编码格式传输数据(参数)
            "Content-Type": "application/x-www-form-urlencoded"
        }
    }).then(function(response) {
        return response.json(); //转json格式返回值
    }).then(function(data) {
        let Arr = data.result.songs;
        if (inp.value != '') {
            inp.value = '';
        }
        Arr.forEach(function(item) {
            // console.log(item);
            let tr = document.createElement("tr");
            tr.innerHTML = `<td>${item.name}</td>
                <td>${item.artists[0].name}</td>
                <td>${item.album.name}</td>
                <td>${moment(item.duration).format('mm:ss')}</td>`;
            tbody.appendChild(tr);
            tr.firstElementChild.addEventListener("click", function() {
                //GET请求
                fetch(`https://music.0melon0.cn/song/url?id=${item.id}`, {
                    method: "GET", //type请求类型；
                    headers: { //设置请求头，表单编码格式传输数据(参数)
                        "Content-Type": "application/x-www-form-urlencoded"
                    }
                }).then(function(response) {
                    return response.json(); //转json格式返回值
                }).then(function(data) {
                    // console.log(data.data[0].url);
                    let music = data.data[0].url;
                    audio.src = music;
                }).catch(function(error) {
                    console.log(error); //获取请求失败的结果
                })
            })
        });
    }).catch(function(error) {
        console.log(error); //获取请求失败的结果
    })
}


//next下一页
next.addEventListener("click", function() {

})

//上一页
prev.addEventListener("click", function() {

})